package com.tlgen.orm.components.autoFill;

import com.tlgen.orm.enums.FillType;
import com.tlgen.orm.utils.orm.AutoFillUtils;

import java.util.List;

public interface AutoFillHandler {

    void save(Object entity);

    void update(Object entity);

    void logicalDelete(Object entity);

    default void saveBatch(List<?> tList) {
        for (Object entity : tList) {
            save(entity);
        }
    }

    default void updateBatch(List<?> tList) {
        for (Object entity : tList) {
            update(entity);
        }
    }

    /**
     * 设置字段值(自动感知操作类型)
     * @param entity 实体对象
     * @param fieldName 字段名
     * @param fieldValue 字段值
     */
    default void setFieldValue(Object entity, String fieldName, Object fieldValue) {
        FillType currentType = OperationContext.getOperationType();
        if (currentType == null) {
            throw new IllegalStateException("Operation type not set");
        }
        AutoFillUtils.setFieldValue(entity, fieldName, fieldValue, currentType);
    }

}
